
package presentation;

import java.rmi.RemoteException ;
import java.text.DateFormat ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.Scanner ;
import java.util.Set ;
import java.util.Vector ;

import persistence.CorruptDataException ;
import persistence.Definitions ;
import persistence.PersistenceController ;
import persistence.hibernate.HibernatePersistenceFactory ;
import util.Bug ;
import util.DuplicationException ;
import util.Log ;
import util.NotFoundException ;
import util.Subsystem ;
import domain.ForumData ;
import domain.LocalForum ;
import domain.searchEngine.SearchEngineFactory ;
import domain.searchEngine.SearchResult ;
import domain.searchEngine.origin.OriginSearchEngineFactory ;
import domain.user.UnpermittedActionException ;

/**
 * User Interface class.
 * listening to all inputs from user and operates the Facade accordingly
 */
public class CommandLineInterface
{

	private static final String INDEX_URI = Definitions.INDEX_URI ;

	/** facade object to operate the appropriate method from the controllers **/
	private final LocalForum forum;

	/** represents the state of the UI in communication with the user
	 * (simulates a state machine) **/
	private String currentState;

	/** the id of the connected user **/
	private long userId;

	/** the id of current presented location (directory, thread or post) **/
	private long currentLocationId;

	/** currently presented threads **/
	private Vector<UIThread> presentedThreads;

	/** currently presented subDirectories **/
	private Vector<UIDirectory> presentedDirectories;

	/** currently presented subDirectories  **/
	private Vector<UIPost> presentedPosts;

	/** instance of Scanner object, that listens to the console */
	private final Scanner console;

	/**
	 * builds the UserInterface and runs the forum.
	 * @param args - array of all file names for the URIs:
	 * args[0] - usersURI
	 * args[1] - postsURI
	 * args[2] - threadsURI
	 * args[3] - directorisURI
	 * args[4] - indexURI
	 *
	 */
	public static void main(final String[] args)
	{
		//initializeNewForum(); //unComment to create a new fresh forum

		try
		{
			//setting up the UserInterface:
			final CommandLineInterface ui = new CommandLineInterface(args[0], args[3], args[2], args[1], args[4]);
			ui.run();
			ui.shutdown();
		}
		catch(final CorruptDataException e)
		{
			System.out.println("could not build forum") ;
		}
		catch (final RemoteException e)
		{
			System.out.println("unable to connect to server.") ;
		}
		System.exit(0) ;
	}

	/**
	 * constructor  - uses local forum and not communication!
	 *
	 * @param usersUri - URI for loading users
	 * @param directoriesUri - URI for loading directories
	 * @param threadsUri - URI for loading threads
	 * @param postUri - URI for loading posts
	 * @param indexUri - URI for loading index
	 * @throws CorruptDataException if there was a problem to load one of the components
	 * @throws RemoteException if connection to the forum has failed.
	 */
	public CommandLineInterface(final String usersUri, final String directoriesUri,
		final String threadsUri, final String postUri, final String indexUri)
		throws CorruptDataException, RemoteException
	{
		//setting up index:
		//final Index index = new OriginIndex(indexUri) ;

		//setting up forum:
		//PersistenceBuilder persistence = new XStreamPersistenceBuilder(PersistenceController.LOAD, usersUri, directoriesUri, threadsUri, postUri) ;
		HibernatePersistenceFactory persistence = new HibernatePersistenceFactory(PersistenceController.LOAD) ;
		final ForumData forumData = new ForumData(persistence);

		//setting up facade:
		SearchEngineFactory searchProvider = new OriginSearchEngineFactory(CommandLineInterface.INDEX_URI, forumData) ;
		//SearchBuilder searchProvider = new CompassSearchBuilder(persistence, forumData) ;
		this.forum = new LocalForum(forumData, searchProvider);

		//get the root directory (to present main page):
		this.currentLocationId = this.forum.getRootDirectory().getId();

		//initialize state to "directory":
		this.currentState = "directory";

		//initialize console to listen to standard input:
		this.console = new Scanner(System.in);

		this.presentedDirectories = new Vector<UIDirectory>();
		this.presentedPosts = new Vector<UIPost>();
		this.presentedThreads = new Vector<UIThread>();
	}


	/**
	 * listening to user inputs and operating accordingly, by calling private methods.
	 * each private method is responsible to operate the appropriate method from Facade,
	 * and to catch and handle all exceptions and bugs that might occur.
	 */
	public void run()
	{
		boolean toExit = false;

		//register as a guest and initialize this.userId:
		this.userId = this.forum.registerGuest().getId();

		//present welcome message and main directory:
		System.out.println("Welcome to WSEP forum!") ;
		this.presentCurrDirectory();

		//the state machine is running until user wants to exit:
		while(!toExit)
		{
			//parse command and process it with the state machine:
			System.out.print("forum> ") ;
			final String line = this.console.nextLine();
			final String[] words = line.split(" ");

			if(words.length!=0)
			{
				if(line.equals("register")) //can be done on any state of the system
				{
					//register:
					this.register();
				}
				else if(line.equals("login")) //can be done on any state of the system
				{
					//login:
					this.login();
				}
				else if(line.equals("logout")) //can be done on any state of the system
				{
					//logout:
					this.logout();
				}
				else if(line.equals("exit")) //can be done on any state of the system
				{
					this.forum.exitUser(this.userId);
					System.out.println("fine, leave.\ni'll just sit here... alone... in the dark... miserable... crying... dont mind me...");
					toExit = true;
				}
				else if(line.equals("home")) //can be done on any state of the system
				{
					//go back to root directory:
					this.currentLocationId = this.forum.getRootDirectory().getId();
					this.presentCurrDirectory();
					this.currentState = "directory";
				}
				else if(words[0].equals("makemoderator") & words.length>1) //can be done on any state of the system
				{
					//assign user (by username) to be a moderator:
					final String username = this.unsplit(words, 1); //for case that the user name is more then one word
					this.makeModerator(username);
				}
				else if(words[0].equals("makeadmin") & words.length>1) //can be done on any state of the system
				{
					//assign user (by username) to be an administrator:
					final String username = this.unsplit(words, 1); //for case that the user name is more then one word
					this.makeAdmin(username);
				}
				else if(words[0].equals("searchaut") & words.length>1) //can be done on any state of the system
				{
					//search by author:
					final String username = this.unsplit(words, 1); //for case that the user name is more then one word
					this.searchByAuthor(username);
					this.currentState = "search";
				}
				else if(words[0].equals("search") & words.length>1) //can be done on any state of the system
				{
					//search by content:
					final String content = this.unsplit(words, 1); //for case that the content is more then one word
					this.searchByContent(content);
					this.currentState = "search";
				}
				else if(words[0].equals("cd") & this.currentState.equals("directory") & words.length>1)
				{
					//change to subDirectory:
					final String dirname = this.unsplit(words, 1); //for case that the dir name is more then one word
					this.changeDirectory(dirname);
					this.currentState = "directory";
				}
				else if(words[0].equals("read") & this.currentState.equals("directory") & words.length>1)
				{
					//read a thread in the directory:
					final String topic = this.unsplit(words, 1); //for case that the topic is more then one word
					this.readThread(topic);
					this.currentState = "thread";
				}
				else if(words[0].equals("edit") & this.currentState.equals("thread") & words.length==2)
				{
					//edit a thread's post:
					try
					{
						final int postIndex = Integer.parseInt(words[1]);
						this.editPost(postIndex);
						this.presentCurrThread(); //present updated posts
						this.currentState = "thread";
					}
					catch(final Exception e)
					{
						//words[1] is not an int
						System.out.println("ilegal command") ;
					}
				}
				else if(line.equals("edittopic") & this.currentState.equals("thread"))
				{
					this.editTopic();
					this.currentState = "thread";
				}
				else if(words[0].equals("adddir") & this.currentState.equals("directory") & words.length>1)
				{
					//add a subDirectory:
					final String name = this.unsplit(words, 1); //for case that the name is more then one word
					this.addDirectory(name);
					this.currentState = "directory";
				}
				else if(words[0].equals("delete") & this.currentState.equals("directory") & words.length>1)
				{
					//delete a directory's thread:
					final String topic = this.unsplit(words, 1); //for case that the topic is more then one word
					this.deleteThread(topic);
					this.currentState = "directory";
				}
				else if(words[0].equals("delete") & this.currentState.equals("thread") & words.length==2)
				{
					//delete a thread's post:
					try
					{
						final int postIndex = Integer.parseInt(words[1]);
						this.deletePost(postIndex);
						this.presentCurrThread(); //present updated posts
						this.currentState = "thread";
					}
					catch(final Exception e)
					{
						//words[1] is not an int
						System.out.println("ilegal command");
					}
				}
				else if(line.equals("addtopic") & this.currentState.equals("directory"))
				{
					//add a new thread in directory:
					this.addThread();
					this.currentState = "directory";
				}
				else if(line.equals("reply") & this.currentState.equals("thread"))
				{
					//add a new post to a thread:
					this.addPost();
					this.presentCurrThread(); //present updated posts
					this.currentState = "thread";
				}
				else if (line.equals("ls") & this.currentState.equals("directory"))
				{
					 this.presentCurrDirectory() ;
				}
				else if (line.equals("statistics"))
				{
					this.getStatistics() ;
				}
				else
				{
					System.out.println("ilegal command") ;
				}
			}
		}//end of while
	}


	//private methods:

	/**
	 * shutting down the system
	 */
	public void shutdown()
	{
		this.forum.exit() ;
	}

	private void addDirectory(final String newDirName)
	{
		try
		{
			final UIDirectory newDir = this.forum.addDirectory(this.userId, this.currentLocationId, newDirName);
			this.presentedDirectories.add(newDir);
			System.out.println("new directory "+newDirName+" was added to current directory") ;
		}
		catch (final UnpermittedActionException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+this.userId+" has tried to add directory without permission");
			System.out.println("operation denied. you are not allowed to add a new directory!") ;
		}
		catch (final NotFoundException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find the directory "+this.currentLocationId+" in the system for adding subdirectory.");
			System.out.println("operation failed. cannot find the current directory in the system") ;
		}
	}

	private void addPost()
	{
		try
		{
			System.out.println("enter a message to reply this topic:");
			final String msg = this.console.nextLine();
			final UIPost post = this.forum.addPost(this.currentLocationId, this.userId, msg);
			this.presentedPosts.add(post);
			System.out.println("new reply message was added successfully") ;
		}
		catch (final UnpermittedActionException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+this.userId+" has tried to add post without permission");
			System.out.println("operation denied. you are not allowed to reply a topic!") ;
		}
		catch (final NotFoundException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find the thread "+this.currentLocationId+" in the system for adding posts.");
			System.out.println("operation failed. cannot find the current directory in the system") ;
		}
	}

	private void addThread()
	{
		try
		{
			System.out.println("enter a new topic:");
			final String newTopic = this.console.nextLine();
			System.out.println("enter first message for your new topic:");
			final String msg = this.console.nextLine();
			final UIThread thread = this.forum.addThread(this.currentLocationId, this.userId, newTopic, msg);
			this.presentedThreads.add(thread);
			System.out.println("new topic "+newTopic+" was added to current directory") ;
		}
		catch (final UnpermittedActionException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+this.userId+" has tried to add thread without permission");
			System.out.println("operation denied. you are not allowed to add a new topic!") ;
		}
		catch (final NotFoundException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find the directory "+this.currentLocationId+" in the system for adding thread.");
			System.out.println("operation failed. cannot find the current directory in the system") ;
		}
	}

	private void changeDirectory(final String name)
	{
		final UIDirectory dir = this.findDir(name);
		if(dir!=null)
		{
			this.currentLocationId = dir.getId();
			System.out.println("\ndirectory "+dir.getName()+": ") ;
			this.presentCurrDirectory();
		}
		else
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).fine("user has asked to present directory "+name+" which does not exist.") ;
			System.out.println("directory "+name+" does not exist under current directory") ;
		}
	}


	private void deletePost(final int index)
	{
		final UIPost post = this.findPost(index);
		if(post!=null)
		{
			try
			{
				this.forum.deletePost(this.userId, post.getId(), this.currentLocationId);
				this.presentedPosts.remove(post);
				System.out.println("post "+index+" was deleted from current thread") ;
			}
			catch (final UnpermittedActionException e)
			{
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+this.userId+" has tried to delete post without permission");
				System.out.println("operation denied. you are not allowed to delete a post!") ;
			}
			catch (final NotFoundException e)
			{
				//if post was already removed from system: remove it from presentation
				this.presentedThreads.remove(post);
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find the post "+post.getId()+" in the system for deleting it.");
			}
		}
		else
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find post "+index+" under thread "+this.currentLocationId+", for deleting it.");
		}
	}


	private void deleteThread(final String topic)
	{
		final UIThread thread = this.findThread(topic);
		if(thread!=null)
		{
			try
			{
				this.forum.deleteThread(this.userId, this.currentLocationId, thread.getId());
				this.presentedThreads.remove(thread);
				System.out.println("topic "+topic+" was deleted from current directory") ;
			}
			catch (final UnpermittedActionException e)
			{
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+this.userId+" has tried to delete thread without permission");
				System.out.println("operation denied. you are not allowed to delete a topic!") ;
			}
			catch (final NotFoundException e)
			{
				//if thread was already removed from system: remove it from presentation
				this.presentedThreads.remove(thread);
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find the thread "+thread.getId()+" in the system for deleting it.");
			}
		}
		else
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find the thread "+topic+" for deleting it.");
		}
	}


	private void editPost(final int index)
	{
		final UIPost post = this.findPost(index);
		if(post!=null)
		{
			System.out.println("enter new message for this post:");
			final String newMessage = this.console.nextLine();
			try
			{
				this.forum.editPost(post.getId(), this.userId, newMessage);
			}
			catch(final UnpermittedActionException e)
			{
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+this.userId+" has tried to edit post without permission");
				System.out.println("operation denied. you are not allowed to edit this post!") ;
			}
			catch(final NotFoundException e)
			{
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find the post "+post.getId()+" in the system for editting it.");
				System.out.println("operation failed. cannot find the post in the system") ;
			}
		}
		else
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).fine("user has asked to present post "+index+" which does not exist.") ;
			System.out.println("post "+index+" does not exist under current topic") ;
		}
	}


	private void editTopic()
	{
		System.out.println("enter new topic for this thread:");
		final String newTopic = this.console.nextLine();
		try
		{
			this.forum.editThreadTopic(this.currentLocationId, this.userId, newTopic);
			System.out.println("operation completed successfully") ;
		}
		catch(final UnpermittedActionException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+this.userId+" has tried to edit topic without permission");
			System.out.println("operation denied. you are not allowed to edit this topic!") ;
		}
		catch(final NotFoundException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find the thread "+this.currentLocationId+" in the system for editting its topic.");
			System.out.println("operation failed. cannot find the thread in the system") ;
		}
	}


	private UIDirectory findDir(final String name)
	{
		for(int i=0; i<this.presentedDirectories.size(); i++)
		{
			final UIDirectory dir = this.presentedDirectories.elementAt(i);
			if((dir.getName()).equals(name))
			{
				return dir;
			}
		}
		return null;
	}

	private UIPost findPost(final int index)
	{
		if(index < this.presentedPosts.size())
		{
			return this.presentedPosts.elementAt(index);
		}
		return null;
	}

	private UIThread findThread(final String topic)
	{
		for(int i=0; i<this.presentedThreads.size(); i++)
		{
			final UIThread thread = this.presentedThreads.elementAt(i);
			if((thread.getTopic()).equals(topic))
			{
				return thread;
			}
		}
		return null;
	}

	/**
	 * formatting a given Date time for a nice presented string:
	 */
	private String getNiceTime(final Date date)
	{
		final DateFormat timeFormat = new SimpleDateFormat("MM-d-HH:mm");
		final String nicetime = timeFormat.format(date);
		return nicetime;
	}

	private void getStatistics()
	{
		try
		{
			System.out.println("there are " + this.forum.getActiveGuests() + " users.") ;
			System.out.println("active members: " + this.forum.getActiveMemberNames()) ;
			long admin = this.forum.getMemberId("admin") ;
			System.out.println("admin statistics: " + this.forum.getPostStatisticsForMember(admin)) ;
			System.out.println("post statistics: " + this.forum.getPostStatistics()) ;
			System.out.println("user statistics: " + this.forum.getUserStatistics()) ;
		}
		catch (NotFoundException e)
		{
			throw new Bug("no admin?") ;
		}

	}

	private void login()
	{
		System.out.println("username:") ;
		final String name = this.console.nextLine();
		System.out.println("password:") ;
		final String password = this.console.nextLine();
		try
		{
			this.userId = this.forum.login(name, password).getId();
			Log.getLogger(Subsystem.PRESENTATION_MODEL).fine("member "+name+" has logged-in.") ;
			System.out.println("welcom "+name+"! you are currently logged-in") ;
		}
		catch(final Exception e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("loggin failed. wrong username or password for username "+name+".") ;
			System.out.println("operation failed. wrong username or password") ;
		}
	}


	private void logout()
	{
		try
		{
			this.forum.logout(this.userId);
			Log.getLogger(Subsystem.PRESENTATION_MODEL).fine("member "+this.userId+" has logged-out.") ;
		}
		catch(final NotFoundException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("logout failed. member with id "+this.userId+" does not exists.") ;
		}
		catch (UnpermittedActionException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("logout failed. member with id "+this.userId+" not a member.") ;
		}

		this.userId = this.forum.registerGuest().getId();
		//the user is logged off anyway, even when the user is not found in the system
		System.out.println("you are currently logged-off") ;
	}

	private void makeAdmin(final String username)
	{
		try
		{
			this.forum.addAdministratorPrivileges(this.userId, username);
			System.out.println("user "+username+" is now administrator.");
		}
		catch(final UnpermittedActionException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+this.userId+" has tried to give privileges without permission");
			System.out.println("operation denied. you are not allowed to give privileges!") ;
		}
		catch(final NotFoundException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find the user "+username+" in the system for assigning him to administrator.");
			System.out.println("operation failed. cannot find a user named "+username);
		}
	}


	private void makeModerator(final String username)
	{
		try
		{
			this.forum.addAdministratorPrivileges(this.userId, username);
			System.out.println("user "+username+" is now moderator.");
		}
		catch(final UnpermittedActionException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+this.userId+" has tried to give privileges without permission");
			System.out.println("operation denied. you are not allowed to give privileges!") ;
		}
		catch(final NotFoundException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("cannot find the user "+username+" in the system for assigning him to moderator.");
			System.out.println("operation failed. cannot find a user named "+username);
		}
	}

	private void presentCurrDirectory()
	{
		Log.getLogger(Subsystem.PRESENTATION_MODEL).finest("directory "+this.currentLocationId+" content is presented.") ;
		try
		{
			this.presentedDirectories = this.forum.getSubDirectories(this.currentLocationId);
			for(int i=0; i<this.presentedDirectories.size(); i++)
			{
				final UIDirectory subdir = this.presentedDirectories.elementAt(i);
				System.out.println("dir: "+subdir.getName());
			}
		}
		catch(final NotFoundException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("problem with presenting directory "+this.currentLocationId+" subDirectories.") ;
			System.out.println("cannot present directory's subDirectories") ;
		}
		try
		{
			this.presentedThreads = this.forum.getThreads(this.currentLocationId);
			for(int i=0; i<this.presentedThreads.size(); i++)
			{
				final UIThread thread = this.presentedThreads.elementAt(i);
				System.out.println("topic: "+thread.getTopic());
			}
		}
		catch(final NotFoundException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("problem with presenting directory "+this.currentLocationId+" threads.") ;
			System.out.println("cannot present directory's subDirectories") ;
		}
		System.out.println("") ;
	}

	private void presentCurrThread()
	{
		Log.getLogger(Subsystem.PRESENTATION_MODEL).finest("thread "+this.currentLocationId+" content is presented.") ;
		try
		{
			this.presentedPosts = this.forum.getPosts(this.currentLocationId);
			this.presentPosts(this.presentedPosts);
		}
		catch(final NotFoundException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("problem with presenting thread "+this.currentLocationId+" posts.") ;
			System.out.println("cannot present posts under this topic");
		}
	}

	private void presentPosts(final Vector<UIPost> posts)
	{
		for(int i=0; i<posts.size(); i++)
		{
			final UIPost post = posts.elementAt(i);
			System.out.println("\npost number: "+i);
			try
			{
				final String author = (this.forum.getMemberById(post.getWriterId())).getUsername();
				System.out.println("author: "+author);
			}
			catch(final NotFoundException e)
			{
				Log.getLogger(Subsystem.PRESENTATION_MODEL).info("problem to find post "+post.getId()+" author with userId "+post.getWriterId()+".") ;
			}
			System.out.println("written at: "+this.getNiceTime(post.getDate()));
			System.out.println(post.getMessage()) ;
		}
		System.out.println("") ;
	}

	private void readThread(final String topic)
	{
		final UIThread thread = this.findThread(topic);
		if(thread!=null)
		{
			this.currentLocationId = thread.getId();
			System.out.println("\ntopic: "+thread.getTopic()) ;
			this.presentCurrThread();
		}
		else
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).fine("user has asked to present topic "+topic+" which does not exist.") ;
			System.out.println("topic "+topic+" does not exist under current directory") ;
		}
	}

	private void register()
	{
		System.out.println("username:") ;
		final String name = this.console.nextLine();
		System.out.println("password:") ;
		final String password = this.console.nextLine();
		try
		{
			this.forum.register(name, password);
			Log.getLogger(Subsystem.PRESENTATION_MODEL).fine("a new registered member: "+name) ;
			System.out.println("registration was completed. to use your new membership please login.") ;
		}
		catch(final DuplicationException e)
		{
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("guest "+this.userId+" trying to register an existing name.") ;
			System.out.println("operation failed. username is already exists") ;
		}
	}



	private void searchByAuthor(final String username)
	{
		final Set<SearchResult> posts = this.forum.searchByAuthor(username);
		final Vector<UIPost> postsVector = new Vector<UIPost>();
		for(SearchResult res : posts)
		{
			postsVector.add(res.getPost());
		}
		System.out.println(""+postsVector.size()+" results found for \""+ username+"\":") ;
		this.presentPosts(postsVector);
	}

	private void searchByContent(final String content)
	{
		final Set<SearchResult> posts = this.forum.searchByContent(content);
		final Vector<UIPost> postsVector = new Vector<UIPost>();
		for(SearchResult res : posts)
		{
			postsVector.add(res.getPost());
		}
		System.out.println(""+postsVector.size()+" results found for \""+content+"\":") ;
		this.presentPosts(postsVector);
	}


	/**
	 * precondition: word.length >= from+1
	 */
	private String unsplit(final String[] words, final int from)
	{
		String res = words[from];
		for(int i=from+1; i<words.length; i++)
		{
			res = res + " " + words[i];
		}
		return res;
	}



}

